angular.module("app", ["ui.router", "ngSanitize", "ngResource", "ui.bootstrap", "dx"]);
angular.module("app").config(["$stateProvider", "$urlRouterProvider", "$locationProvider", "$httpProvider", function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.transformResponse = function (data, headersGetter, status) {
        if (status !== 200) {
            alert("服务器开了小差, 请刷新或联系管理员");
            sessionStorage.clear();
            location.href = "#/login";
            return;
        }
        if (headersGetter()["content-type"] == "application/json;charset=UTF-8") {
            data = angular.fromJson(data);
            if (location.hash == "#/login") return data;
            if (data.statusCode == -100) {
                alert("系统服务连接异常!");
                location.href = "#/login";
            } else if (data.statusCode == -1) {
                alert("用户未登录");
                location.href = "#/login";
            }
        }
        return data;
    };
    $urlRouterProvider.when("", "/login");
    $urlRouterProvider.otherwise("/home/404");
    $stateProvider
        .state("login", {
            url: "/login",
            views: {
                "mainContent": {
                    templateUrl: "template/login/login.html?tk=" + new Date().getTime(),
                    controller: "loginCtrl"
                }
            }
        })
        .state("home", {
            url: "/home",
            views: {
                "mainContent": {
                    templateUrl: "template/home/home.html?tk=" + new Date().getTime(),
                    controller: "homeCtrl"
                }
            },
            resolve: {
                "treeViewDataSource": ["org", function (org) {
                    return org.getTreeData({}, {test: 1}, function (data) {
                    }).$promise;
                }],
                "orgList": ["org","treeViewDataSource", function (org,treeViewDataSource) {
                    return org.getList({}, {orgId: JSON.parse(sessionStorage.userInfo).orgId}, function (data) {
                    }).$promise;
                }]
            }
        })
        .state("resetPassword", {
            url: "/resetPassword",
            views: {
                "mainContent": {
                    templateUrl: "template/resetPassword/resetPassword.html?tk=" + new Date().getTime(),
                    controller: "resetPassword.Ctrl"
                }
            }
        })
        .state("home.welcome", {
            url: "/welcome",
            views: {
                "viewContent": {
                    templateUrl: "template/home/welcome/welcome.html?tk=" + new Date().getTime(),
                    controller: "welcomeCtrl"
                }
            }
        })
        .state("home.welcome.group", {
            url: "/group",
            views: {
                "viewContent": {
                    templateUrl: "template/home/welcome/group/group.html?tk=" + new Date().getTime(),
                    controller: "welcome.group.Ctrl"
                }
            }
        })
        .state("home.welcome.volunteer", {
            url: "/volunteer",
            views: {
                "viewContent": {
                    templateUrl: "template/home/welcome/volunteer/volunteer.html?tk=" + new Date().getTime(),
                    controller: "welcome.volunteer.Ctrl"
                }
            }
        })
        .state("home.welcome.activity", {
            url: "/activity",
            views: {
                "viewContent": {
                    templateUrl: "template/home/welcome/activity/activity.html?tk=" + new Date().getTime(),
                    controller: "welcome.activity.Ctrl"
                }
            }
        })
        .state("home.welcome.time", {
            url: "/time",
            views: {
                "viewContent": {
                    templateUrl: "template/home/welcome/time/time.html?tk=" + new Date().getTime(),
                    controller: "welcome.time.Ctrl"
                }
            }
        })
        .state("home.setting", {
            url: "/setting",
            views: {
                "viewContent": {
                    templateUrl: "template/home/setting/setting.html?tk=" + new Date().getTime(),
                    controller: "settingCtrl"
                }
            }
        })
        .state("home.org", {
            url: "/org",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.org.list", {
            url: "/list",
            views: {
                "subview": {
                    templateUrl: "template/home/org/list/list.html?tk=" + new Date().getTime(),
                    controller: "org.list.Ctrl"
                }
            }
        })
        .state("home.org.info", {
            url: "/info",
            views: {
                "subview": {
                    templateUrl: "template/home/org/info/info.html?tk=" + new Date().getTime(),
                    controller: "org.info.Ctrl"
                }
            }
        })
        .state("home.org.managerList", {
            url: "/managerList",
            views: {
                "subview": {
                    templateUrl: "template/home/org/managerList/managerList.html?tk=" + new Date().getTime(),
                    controller: "org.managerList.Ctrl"
                }
            }
        })
        .state("home.group", {
            url: "/group",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.group.list", {
            url: "/list",
            views: {
                "subview": {
                    templateUrl: "template/home/group/list/list.html?tk=" + new Date().getTime(),
                    controller: "group.list.Ctrl"
                }
            }
        })
        .state("home.group.verify", {
            url: "/verify",
            views: {
                "subview": {
                    templateUrl: "template/home/group/verify/verify.html?tk=" + new Date().getTime(),
                    controller: "group.verify.Ctrl"
                }
            }
        })
        .state("home.passport", {
            url: "/passport",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.passport.make", {
            url: "/make",
            views: {
                "subview": {
                    templateUrl: "template/home/passport/make/make.html?tk=" + new Date().getTime(),
                    controller: "passport.make.Ctrl"
                }
            }
        })
        .state("home.passport.allot", {
            url: "/make/:id",
            views: {
                "subview": {
                    templateUrl: "template/home/passport/make/allot.html?tk=" + new Date().getTime(),
                    controller: "passport.make.Ctrl"
                }
            }
        })
        .state("home.passport.send", {
            url: "/send",
            views: {
                "subview": {
                    templateUrl: "template/home/passport/send/send.html?tk=" + new Date().getTime(),
                    controller: "passport.send.Ctrl"
                }
            }
        })
        .state("home.passport.sent", {
            url: "/sent",
            views: {
                "subview": {
                    templateUrl: "template/home/passport/sent/sent.html?tk=" + new Date().getTime(),
                    controller: "passport.sent.Ctrl"
                }
            }
        })
        .state("home.passport.activate", {
            url: "/activate",
            views: {
                "subview": {
                    templateUrl: "template/home/passport/activate/activate.html?tk=" + new Date().getTime(),
                    controller: "passport.activate.Ctrl"
                }
            }
        })
        .state("home.passport.logout", {
            url: "/logout",
            views: {
                "subview": {
                    templateUrl: "template/home/passport/logout/logout.html?tk=" + new Date().getTime(),
                    controller: "passport.logout.Ctrl"
                }
            }
        })
        .state("home.group.base", {
            url: "/base",
            views: {
                "subview": {
                    templateUrl: "template/home/group/base/base.html?tk=" + new Date().getTime(),
                    controller: "group.base.Ctrl"
                }
            }
        })
        .state("home.group.baseDetail", {
            url: "/base/:id",
            views: {
                "subview": {
                    templateUrl: "template/home/group/base/base-detail.html?tk=" + new Date().getTime(),
                    controller: "group.base.Ctrl"
                }
            }
        })
        .state("home.group.volunteerList", {
            url: "/volunteerList/:groupId",
            views: {
                "subview": {
                    templateUrl: "template/home/group/volunteerList/volunteerList.html?tk=" + new Date().getTime(),
                    controller: "group.volunteerList.Ctrl"
                }
            }
        })
        .state("home.group.activityList", {
            url: "/activityList/:userId",
            views: {
                "subview": {
                    templateUrl: "template/home/group/activityList/activityList.html?tk=" + new Date().getTime(),
                    controller: "group.activityList.Ctrl"
                }
            }
        })
        .state("home.group.info", {
            url: "/info/:groupId",
            views: {
                "subview": {
                    templateUrl: "template/home/group/info/info.html?tk=" + new Date().getTime(),
                    controller: "group.info.Ctrl"
                }
            }
        })
        .state("home.user", {
            url: "/user",
            abstract: true,
            views: {
                "viewContent": {
                    template: "<div ui-view='subview'><div/>"
                }
            }
        })
        .state("home.user.changePassword", {
            url: "/changePassword",
            views: {
                "subview": {
                    templateUrl: "template/home/user/changePassword/changePassword.html?tk=" + new Date().getTime(),
                    controller: "user.changePassword.Ctrl"
                }
            }
        });
    $httpProvider.interceptors.push("timestampMarker");
}]);